package com.flex.showdoc4j.template;

import lombok.extern.slf4j.Slf4j;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.FileResourceLoader;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @author ganlt<br>
 * @since 2019/06/27 10:15 <br>
 */
@Slf4j
public class BeetlTemplate implements Template {
    static Configuration cfg;

    static {
        try {
            cfg = Configuration.defaultConfiguration();
            log.info("字符串模板解析");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String stringTemplateRender(String templateStr, Map<String, Object> bindMap) {
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        org.beetl.core.Template t = gt.getTemplate(templateStr);
        t.binding(bindMap);
        return t.render();
    }

    @Override
    public String fileTemplateRender(String fileName, Map<String, Object> bindMap) {
     //   InputStream root = BeetlTemplate.class.getResourceAsStream();
        ResourceLoader resourceLoader = new ClasspathResourceLoader("template/", "utf8");
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        org.beetl.core.Template t = gt.getTemplate(fileName);
        t.binding(bindMap);
        return t.render();
    }
}
